// pages/classify/classify.js
import { fenlei, fenleigoods, addshop } from "../../http/http";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    left: [],
    right: [],
    goods: [],
    key: 1,
    value:""
  },
  a(key) {
    this.data.goods = []
    this.setData({
      goods: this.data.goods
    })
    console.log(key)
    for (let i = 0; i < this.data.right.length; i++) {
      if (key.target.dataset.key[1] == this.data.right[i].categoryId) {
        this.data.goods.push(this.data.right[i])
        this.setData({
          goods: this.data.goods
        })
      }
    }
    this.setData({
      key: key.target.dataset.key[1]
    })
  },
  // 添加购物车
  addshop(id) {
    // console.log(id.target.dataset.id.id)
    addshop({ goodsId: id.target.dataset.id.id, number: 1, token: wx.getStorageSync('token').token }).then(res => {
      console.log(res)
      if (res.msg == "success") {
        wx.showToast({
          title: "添加购物车成功",
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  // 搜索
  search(){
    wx.navigateTo({
      url: `/pages/search/search?value=${this.data.value}`
    })
  },
  // 搜索内容发生改变
  changesearch(value){
    console.log(value.detail)
    this.setData({
      value:value.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        key: options.id
      })
      // 左侧列表
      fenlei(wx.getStorageSync('token').token).then(res => {
        console.log(res)
        this.setData({
          left: res.data,
        })
        // 商品列表
        fenleigoods(wx.getStorageSync('token').token).then(res => {
          console.log(res)
          this.setData({
            right: res.data,
          })
          for (let i = 0; i < this.data.right.length; i++) {
            if (options.id == this.data.right[i].categoryId) {
              this.data.goods.push(this.data.right[i])
              this.setData({
                goods: this.data.goods,
              })
            }
          }
        })
      })

    } else {
      // 左侧列表
      fenlei(wx.getStorageSync('token').token).then(res => {
        console.log(res)
        this.setData({
          left: res.data,
        })
        // 商品列表
        fenleigoods(wx.getStorageSync('token').token).then(res => {
          console.log(res)
          this.setData({
            right: res.data,
          })
          for (let i = 0; i < this.data.right.length; i++) {
            if (187367 == this.data.right[i].categoryId) {
              this.data.goods.push(this.data.right[i])
              this.setData({
                goods: this.data.goods,
                key:187367
              })
            }
          }
        })
      })
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})